package jp.sourceforge.akjrcp.ui.param;

import jp.sourceforge.akjrcp.ui.Messages;
import jp.sourceforge.akjrcp.ui.param.AbstractParamCompositeItem.SelectionCheck;
import jp.sourceforge.akjrcp.util.ColorUtils;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;

public class ParamColor extends AbstractParamCompositeItem{

private String key;
private String label;


private Label labelItem;

private String value="";

private Label colorLabel;
private RGB rgb;
private Composite self;
public String getKey() {
	return key;
}

public String getLabel() {
	return label;
}



public void setToolTipText(String text){
	labelItem.setToolTipText(text);
	this.colorLabel.setToolTipText(text);
}




public String getValue() {
	if(rgb==null){
		return "";
	}
	return ""+ColorUtils.rgbToHex(rgb);
}

private void updateColor(String hex){
	this.value=hex;
	rgb=ColorUtils.hexToRGB(hex);
	updateColor();
}
private void updateColor(){
	if(rgb==null){
		colorLabel.setText("  X  ");
		colorLabel.setBackground(null);//is OK?
	}else{
	//	log.info(rgb);
		colorLabel.setText("     ");
		colorLabel.setBackground(new Color(null,rgb));
	}
}
public void setValue(String value) {
	updateColor(value);
	this.value=value;
}
	public ParamColor(Composite parent, int style,String key,String label) {
		super(parent, style);
		self=this;
		this.key=key;
		this.label=label;
		
		this.setLayout(new GridLayout(2,false));
		labelItem = new Label(this,SWT.NONE);
		labelItem.setText(label);
		
		Composite colorItems=new Composite(this,SWT.NULL);
		colorItems.setLayout(new GridLayout(3,false));
		colorLabel=new Label(colorItems,SWT.BORDER);
		colorLabel.setText("  X  ");
		Button colorButton=new Button(colorItems,SWT.NULL);
		colorButton.setText(Messages.getString("choose_color"));
		colorButton.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent e) {
					
			}

			public void widgetSelected(SelectionEvent e) {
				ColorDialog dialog=new ColorDialog(self.getShell());
				if(rgb!=null){
					dialog.setRGB(rgb);
				}
					RGB newrgb = dialog.open();
			        if (newrgb != null) {
			        	Color bg=colorLabel.getBackground();
			        	
			        	if(bg!=null){
			        		bg.dispose();
			        	}
			        	rgb=newrgb;
			        	updateColor();
			        }
				
			}});
		
		colorButton.addSelectionListener(new SelectionCheck());
		
		Button resetButton=new Button(colorItems,SWT.NULL);
		resetButton.setText(Messages.getString("reset_color"));
		resetButton.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent e) {
					
			}

			public void widgetSelected(SelectionEvent e) {
				rgb=null;
				updateColor();
			}});
		
		resetButton.addSelectionListener(new SelectionCheck());
		//GridData gdata=new GridData();
		//gdata.widthHint=30;
		
		//spinnerAndScale.setLayoutData(gdata);
	}


	
	public boolean isModified() {
		
		String oldValue=getValue();
		
		
		
		return !oldValue.equals(value);
	}

	public RGB getRgb() {
		return rgb;
	}

	public void setRgb(RGB rgb) {
		this.rgb = rgb;
	}

	

	

}
